<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="栏目名称" prop="columnName">
        <el-input
          v-model="queryParams.columnName"
          placeholder="请输入栏目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="状态" clearable>
          <el-option
            v-for="dict in dict.type.tb_column_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否开放" prop="isOpen">
        <el-select v-model="queryParams.isOpen" placeholder="是否开放给用户" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
	    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button> -->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:infoColumn:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="infoColumnList"
      row-key="columnId"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <!-- <el-table-column label="上级id" prop="parentId" /> -->
      <el-table-column label="栏目名称" align="center" prop="columnName" />
      <el-table-column label="排序" align="center" prop="orderNum" />
      <el-table-column prop="state" align="center" label="状态" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.tb_column_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column prop="isOpen" align="center" label="是否开放给用户">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isOpen"/>
        </template>
      </el-table-column>
      <el-table-column prop="isPic" align="center" label="是否为图片栏目">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isPic"/>
        </template>
      </el-table-column>
      <el-table-column prop="isIndex" align="center" label="是否展示在首页">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isIndex"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:infoColumn:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['business:infoColumn:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:infoColumn:remove']"
          >删除</el-button>
          <el-button v-if="scope.row.isDetail == 'Y'"
            size="mini"
            type="text"
            icon="el-icon-message"
            @click="handleDetail(scope.row)"
            v-hasPermi="['business:infoColumn:remove']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改栏目信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="上级栏目" prop="parentId">
              <treeselect v-model="form.parentId" :options="infoColumnOptions" :normalizer="normalizer" placeholder="请选择上级栏目" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="栏目名称" prop="columnName">
              <el-input v-model="form.columnName" placeholder="请输入栏目名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="状态" prop="state">
              <el-radio-group v-model="form.state">
                <el-radio label="0">正常</el-radio>
                <el-radio label="1">停用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否为文章栏目" prop="isDetail">
              <el-radio-group v-model="form.isDetail">
                <el-radio label="N">否</el-radio>
                <el-radio label="Y">是</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.isDetail == 'Y'">
          <el-col :span="12">
            <el-form-item label="是否展示在首页" prop="isIndex">
              <el-radio-group v-model="form.isIndex">
                <el-radio label="N">否</el-radio>
                <el-radio label="Y">是</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否为图片栏目" prop="isPic">
              <el-radio-group v-model="form.isPic">
                <el-radio label="N">否</el-radio>
                <el-radio label="Y">是</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否开放给用户" prop="isOpen">
              <el-radio-group v-model="form.isOpen">
                <el-radio label="N">否</el-radio>
                <el-radio label="Y">是</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="orderNum">
              <el-input v-model="form.orderNum" placeholder="请输入排序" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfoColumn, getInfoColumn, delInfoColumn, addInfoColumn, updateInfoColumn } from "@/api/business/infoColumn";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "InfoColumn",
  dicts: ['tb_column_state', 'sys_yes_no'],
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 栏目信息表格数据
      infoColumnList: [],
      // 栏目信息树选项
      infoColumnOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        columnName: null,
        state: null,
        isOpen: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleDetail(row){
      this.$router.push({
        path: "/forum/columnDetail",
        query: { columnId: row.columnId,isPic: row.isPic },
      });
    },
    /** 查询栏目信息列表 */
    getList() {
      this.loading = true;
      listInfoColumn(this.queryParams).then(response => {
        this.infoColumnList = this.handleTree(response.data, "columnId", "parentId");
        this.loading = false;
      });
    },
    /** 转换栏目信息数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.columnId,
        label: node.columnName,
        children: node.children
      };
    },
	/** 查询栏目信息下拉树结构 */
    getTreeselect() {
      listInfoColumn().then(response => {
        this.infoColumnOptions = [];
        const data = { columnId: 0, columnName: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "columnId", "parentId");
        this.infoColumnOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        columnId: null,
        parentId: null,
        columnName: null,
        orderNum: null,
        state: '0',
        isOpen: 'N',
        isDetail: 'N',
        isPic: 'N'
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.columnId) {
        this.form.parentId = row.columnId;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = "添加栏目信息";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.parentId;
      }
      getInfoColumn(row.columnId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改栏目信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.columnId != null) {
            updateInfoColumn(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfoColumn(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除栏目信息编号为"' + row.columnId + '"的数据项？').then(function() {
        return delInfoColumn(row.columnId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
